<?php

/**
 * Created by Atom.
 * User: qiliping
 * Date: 2017/1/15
 * Time: 11:18
 */
class Courseo2o_model extends CI_Model
{

  /**
   * 获取班课list
   * @param  [type]  $school_id [description]
   * @param  integer $limit     [description]
   * @param  integer $offset    [description]
   * @return [type]             [description]
   */
  public function get_banke_list_page($school_id,$cate_id,$limit=0,$offset=10)
  {
      $where = " status = 1 and type =3 and school_id={$school_id}";
      if(!empty($cate_id)&&$cate_id>0){
        $where .= " and cate_id={$cate_id}";
      }

      // print_r($where);exit;
       
      $sql = "SELECT SQL_CALC_FOUND_ROWS * FROM {$this->db->dbprefix}course WHERE $where LIMIT $limit, $offset";
      $result = $this->db->query($sql)->result_array();

      $row_num =  $this->db->query('SELECT FOUND_ROWS() as row_num;')->row_array();
      return array("count" => $row_num['row_num'], "result" => $result);
      

  }

  /**
  * 获取推荐课程
  */
  public function get_banke_list($school_id,$top = 6, $is_recomend = true)
  {
    $sql = "SELECT course.*,school.school_name FROM {$this->db->dbprefix}course course left join {$this->db->dbprefix}o2o_school school on school.id = course.school_id where course.school_id={$school_id} and course.status= 1 and course.type = 3 ";
    if($is_recomend){
      $sql .= "order by course.is_recomend desc limit {$top}";
    }else{
      $sql .= "order by course.id desc limit {$top}";
    }
    return $this->db->query($sql)->result_array();
  }

  /**
   * 获取课程详情
   * @param  [type] $course_id [description]
   * @return [type]            [description]
   */
  public function get_course_detail($course_id)
  {
    //$sql = "SELECT * FROM {$this->db->dbprefix}course course where id = {$course_id}";
    $sql = "SELECT cour.*,cate.name as cate_name,t.teacher_name FROM {$this->db->dbprefix}course cour
        left join {$this->db->dbprefix}course_cate cate on cate.id=cour.cate_id
        left join {$this->db->dbprefix}o2o_teachers t  on t.id = cour.teacher_id 
        where cour.id={$course_id}";
    return $this->db->query($sql)->row_array();
  }

  /**
   * 获取面授课关联的科目list
   * 
   * @param  [type] $course_id [description]
   * @return [type]            [description]
   */
  public function get_course_subject_list($course_id)
  {
    //$sql = "SELECT * FROM {$this->db->dbprefix}course_subject where course_id = {$course_id} ";
    $sql ="SELECT sub.*,ocour.title FROM {$this->db->dbprefix}course_subject sub 
          left join {$this->db->dbprefix}o2o_course ocour on ocour.id = sub.subject_id
          where sub.course_id = {$course_id}";
    return $this->db->query($sql)->result_array();
  }




    /**
   * 获取线下课程分类
   * @param  [type] $school_id [description]
   * @return [type]            [description]
   */
  public function get_offline_cate($school_id)
  {
    $sql = "SELECT * FROM {$this->db->dbprefix}course_cate where school_id = {$school_id} and type = 2 order by sort desc, id desc ";
    return $this->db->query($sql)->result_array();
  }

  function get_class_course($limit = 0, $offset = 10, $conditions = array()){
    $where = "WHERE school_id = '{$this->school_id}' ";
    if(!empty($conditions['class_id'])){
      $where .= " AND class_id = '{$conditions['class_id']}' ";
    }
    if(!empty($conditions['name'])){
      $where .= " AND name LIKE '%{$conditions['name']}%' ";
    }
    $sql = "SELECT SQL_CALC_FOUND_ROWS * FROM {$this->db->dbprefix}o2o_class_courses $where order by id desc LIMIT $limit, $offset";
    $result = $this->db->query($sql)->result_array();
    $row_num =  $this->db->query('SELECT FOUND_ROWS() as row_num;')->row_array();
    return array("total_count" => $row_num['row_num'], "result" => $result);
  }
}
